package cm.mefobe.android.telecomcost.dialog;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.entities.Day;
import cm.mefobe.android.telecomcost.view.DaysCheckListView;
import cm.mefobe.android.telecomcost.view.DaysPickerView;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DaysPickerDialog extends Dialog {

	private Button btnSet;
	private Button btnClose;
	private Context _context;
	private DaysPickerView _pickerView;
	private DaysCheckListView days_list_view;

	public DaysPickerDialog(Context context, DaysPickerView pickerView) {
		super(context);
		_pickerView = pickerView;
		_context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("DaysPickerDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		setContentView(R.layout.days_picker_dialog);
		setTitle("Select days of week");

		days_list_view = (DaysCheckListView) findViewById(R.id.listViewDays);
		btnSet = (Button) findViewById(R.id.btnSave);
		btnClose = (Button) findViewById(R.id.btnClose);
		btnSet.setText("Set");

		btnSet.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				setSelectedDays();
			}
		});

		btnClose.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				closeDialog();
			}
		});

		Log.i("DaysPickerDialog", "Dialog ohne bundle created....");
		days_list_view.setAdapter(null);
	}

	protected void setSelectedDays() {
		ArrayList<Day> selectedDays = days_list_view.getSelectedDays();
		if (selectedDays.size() > 0) {
			_pickerView.setSelectedDays(selectedDays);
			closeDialog();
		}
		else
		{
			Toast.makeText(_context, "You must select at least one week day for this rate", 5).show();
		}
	}

	protected void closeDialog() {
		Log.i("DaysPickerDialog", "closing the dialog");
		hide();
	}

	@Override
	public void show() {
		Log.i("DaysPickerDialog", "showing the dialog...");
		super.show();
		Log.i("DaysPickerDialog", "dialog on screen...");
	}

	@Override
	protected void onStart() {
		Log.i("DaysPickerDialog", "Starting the dialog...");
		super.onStart();
		Log.i("DaysPickerDialog", "dialog started...");
	}

	@Override
	protected void onStop() {
		Log.i("DaysPickerDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("DaysPickerDialog", "dialog stopped...");
	}
}
